package com.sch.study.concurrency;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;

/**
 * User: hui_stone
 * Date: 2016/2/14 0014
 * Time: 上午 10:07
 */
public class ExchangeTest {

    public static void main(String[] args) {
        final Exchanger<List<Integer>> exchanger = new Exchanger<List<Integer>>();
        new Thread(){
            @Override
            public void run(){
                List<Integer> list = new ArrayList<Integer>(2);
                list.add(1);
                list.add(2);
                try {
                    List changelist = exchanger.exchange(list);
                    System.out.println("线程1："+changelist);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run(){
                List<Integer> list = new ArrayList<Integer>(2);
                list.add(3);
                list.add(4);
                try {
                    List changelist = exchanger.exchange(list);
                    System.out.println("线程2"+changelist);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}
